package com.chandra.pos.config.web;

import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.format.FormatterRegistry;
import org.springframework.format.datetime.joda.JodaTimeFormatterRegistrar;
import org.springframework.format.number.NumberFormatAnnotationFormatterFactory;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.web.servlet.config.annotation.DefaultServletHandlerConfigurer;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;

import java.util.List;

/**
 * Created by Admin on 6/27/14.
 * http://www.joshlong.com/jl/blogPost/simplified_web_configuration_with_spring.html
 * http://blog.safaribooksonline.com/2013/09/30/rest-hypermedia/
 * http://stackoverflow.com/questions/17473045/configuring-spring-security-tiles-thymeleaf-using-javaconfig
 */


@Configuration
@ComponentScan("com.chandra.web")
public class WebMvcConfiguration extends WebMvcConfigurerAdapter {


    @Override
    public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) {
        configurer.enable();
    }



    /*

        <mvc:resources location="/images/,classpath:/META-INF/images/" mapping="/images/**" />
        <mvc:resources location="/css/,classpath:/META-INF/css/" mapping="/css/**" />
        <mvc:resources location="/js/,classpath:/META-INF/js/" mapping="/js/**" />

     */


    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/css/**").addResourceLocations(
                "/css/", "classpath:/META-INF/css/");
        registry.addResourceHandler("/js/**").addResourceLocations(
                "/js/", "classpath:/META-INF/js/");
        registry.addResourceHandler("/images/**").addResourceLocations(
                "/images/", "classpath:/META-INF/images/");


    }

    @Override
    public void addFormatters(FormatterRegistry registry) {
        registry.addFormatterForFieldAnnotation(new NumberFormatAnnotationFormatterFactory());

        new JodaTimeFormatterRegistrar().registerFormatters(registry);


    }

    @Override
    public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {


    }
    //TODO Json Request failes

//    @Override
//    public void configureContentNegotiation(
//            ContentNegotiationConfigurer configurer) {
//        configurer.ignoreAcceptHeader(true);
//        Map<String, MediaType> mediaTypes = new HashMap<String, MediaType>();
//        mediaTypes.put("json", MediaType.APPLICATION_JSON);
//        mediaTypes.put("xml", MediaType.APPLICATION_XML);
//        mediaTypes.put("xml2", MediaType.TEXT_XML);
//        mediaTypes.put("xhtml", MediaType.APPLICATION_XHTML_XML);
//        mediaTypes.put("html", MediaType.TEXT_HTML);
//        configurer.mediaTypes(mediaTypes);
//        configurer.defaultContentType(MediaType.TEXT_HTML);
//    }


}
